package com.ecommerce.dal.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.ecommerce.dal.dao.ICategoryDAO;
import com.ecommerce.dal.entity.Category;
import com.ecommerce.dal.util.CustomHibernateDAOSupport;


@Repository("categoryDAO")
public class CategoryDAO extends CustomHibernateDAOSupport implements
		ICategoryDAO {

	@Override
	public void save(Category category) {
		getHibernateTemplate().save(category);

	}

	@Override
	public void update(Category category) {
		getHibernateTemplate().update(category);

	}

	@Override
	public void delete(Category category) {
		getHibernateTemplate().delete(category);

	}

	@Override
	public Category findByCategoryId(long categoryId) {
		List list = getHibernateTemplate().find(
				"from Category category where category.categoryId=?",
				categoryId);
		return (Category) ((list != null && list.size()>0) ? list.get(0) : null) ;
	}

	@Override
	public Category findByCategoryName(String categoryName) {
		List list = getHibernateTemplate()
				.find("select category from Category category where category.categoryName = ?",categoryName);
				//.find("select subCategory from Category category inner join Category subCategory on category.subCategoryId = subCategory.categoryId where subCategory.categoryName = ?", subCategoryName);
		return (Category) ((list != null && list.size()>0) ?  list.get(0) : null) ;
	}

	@Override
	public List<Category> findAllCategories() {
		List list = getHibernateTemplate().find("from Category");
		return (list != null && list.size()>0) ?  list : null ;
	}

	@Override
	public List<Category> findAllSubCategories(long categoryId) {
		List list = getHibernateTemplate()
				.find("select category from Category category inner join category.category superCategory where superCategory.categoryId = ?",categoryId);
		return (list != null && list.size()>0) ?  list : null ;

	}

}
